package com.jhs.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.jhs.boot.common.beans.Common;
import com.jhs.boot.system.converter.SysNodesTrafficConverter;
import com.jhs.boot.system.mapper.SysNodesTrafficMapper;
import com.jhs.boot.system.model.entity.SysNodes;
import com.jhs.boot.system.model.entity.SysNodesTraffic;
import com.jhs.boot.system.model.vo.NodeStatistic;
import com.jhs.boot.system.service.StatisticService;
import com.jhs.boot.system.service.SysNodesService;
import com.jhs.boot.system.service.SysNodesTrafficService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname StatisticServiceIMpl
 * @Description TODO
 * @Date 2025/8/21 12:46
 * @Created by zxy
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class StatisticServiceIMpl  implements StatisticService {
    @Autowired
    private SysNodesService sysNodesService;
    @Autowired
    private SysNodesTrafficService sysNodesTrafficService;
    @Autowired
    private SysNodesTrafficConverter sysNodesTrafficConverter;

    @Autowired
    private SysNodesTrafficMapper sysNodesTrafficMapper;
    @Override
    public NodeStatistic count() {
        //统计主节点信息和主节点下的连接数信息
        NodeStatistic build = null;
        SysNodes nodeStatistic=sysNodesService.getSysNodesByType(Common.ZERO);
        if (ObjectUtils.isNotNull(nodeStatistic)) {
            build = NodeStatistic.builder().ip(nodeStatistic.getIp()).build();
            //查询下面的子节点信息
            List<SysNodesTraffic> list = sysNodesTrafficService.list(new LambdaQueryWrapper<SysNodesTraffic>()
                    .eq(SysNodesTraffic::getNodeId, nodeStatistic.getId()).orderByAsc(SysNodesTraffic::getId));
            if (CollectionUtils.isNotEmpty(list)){
                SysNodesTraffic sysNodesTraffic = list.get(0);
                build=sysNodesTrafficConverter.toNodeStatisticVO(sysNodesTraffic);
                build.setIp(nodeStatistic.getIp());
                List<Long> dept = sysNodesService.getSysNodesDept();
                // 根据id查询 查询客户端的节点总数 在线数 离线数
                build.setOnLineTernimal((double)  sysNodesService.count(new LambdaQueryWrapper<SysNodes>()
                        .in(dept.size()>0,SysNodes::getDeptId, dept)
                        .ge(SysNodes::getType, Common.TWO).eq(SysNodes::getStatus,Common.ONE)
                         ));

                build.setDownLineTernimal((double)  sysNodesService.count(new LambdaQueryWrapper<SysNodes>()
                        .in(dept.size()>0,SysNodes::getDeptId, dept)
                                .ge(SysNodes::getType, Common.TWO).eq(SysNodes::getStatus,Common.ZERO)
                        ));

                //查询map的在线和离线数
                build.setOnLine((double)  sysNodesService.count(new LambdaQueryWrapper<SysNodes>()
                        .in(dept.size()>0,SysNodes::getDeptId, dept)
                        .eq(SysNodes::getType, Common.ONE).eq(SysNodes::getStatus,Common.ONE)
                         ));

                build.setDownLine((double) sysNodesService.count(new LambdaQueryWrapper<SysNodes>()
                        .in(dept.size()>0,SysNodes::getDeptId, dept)
                        .eq(SysNodes::getType, Common.ONE).eq(SysNodes::getStatus,Common.ZERO)
                        ));

            }
        }
        return build;
    }
}
